//axios基础封装
import axios from "axios";
import {ElMessage} from "element-plus";
import {useUserStore} from "@/stores/user";

const httpInstance = axios.create({
    baseURL:'http://localhost:8080',  // 基地址
    timeout:5000    // 超时器
})


//拦截器
httpInstance.interceptors.request.use(config=>{

    // 1. 获取token数据
    const  currentUser=localStorage.getItem("currentUser");
    // 2. 按照后端的要求拼接token数据
    const token = currentUser.userInfo.token
    if(token){
        config.headers.token = `Bearer ${token}`
    }

    return config
},e=>Promise.reject(e))

//响应器
httpInstance.interceptors.response.use(res=> res.data,e=>{
    // 统一错误提示
    ElMessage({
        type:'warning',
        message:e.response.data.message
    })
    return Promise.reject(e)
})

export default httpInstance